package pl.edu.agh.pros.android;

import org.apache.commons.lang.RandomStringUtils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

	public static String deviceId;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		TelephonyManager telephonyManager = (TelephonyManager) getBaseContext().getSystemService(
				Context.TELEPHONY_SERVICE);
		deviceId = telephonyManager.getDeviceId();
		if (deviceId == null) {
			deviceId = RandomStringUtils.randomAlphanumeric(31);
		}

		Button startServerButton = (Button) findViewById(R.id.buttonServer);
		startServerButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(MainActivity.this, ServerActivity.class);
				startActivity(intent);
			}
		});

		Button startClientButton = (Button) findViewById(R.id.buttonClient);
		startClientButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(MainActivity.this, ClientActivity.class);
				startActivity(intent);
			}
		});

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();

	}

	public static boolean isConnected(Context context, ConnectivityManager cm) {

		boolean isConnected = (cm != null) && (cm.getActiveNetworkInfo() != null)
				&& cm.getActiveNetworkInfo().isConnectedOrConnecting();

		if (!isConnected) {
			Toast.makeText(context, "No internet access. Activity could not be started.", Toast.LENGTH_SHORT).show();
		}
		return isConnected;
	}

}
